﻿using System;
using System.Collections.Generic;
using Beyondsoft.Tuanju.DataManager;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Tuan
{
    public sealed partial class CategoryPage : Tuan.Common.LayoutAwarePage
    {
        /// <summary>
        /// Initialize CategoryPage
        /// </summary>
        public CategoryPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Binding context data when loading this page
        /// </summary>
        /// <param name="navigationParameter">Navigation parameter</param>
        /// <param name="pageState">page state</param>
        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.CityName.DataContext = City.DefaultCity;
            DealGroup dealGroup = await DealGroup.GetDealGroupInstance();
            this.DefaultViewModel["Groups"] = dealGroup.Groups.Values;
        }

        /// <summary>
        /// Navigating to CategoryGroupPage when click header button
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">agument</param>
        void Header_Click(object sender, RoutedEventArgs e)
        {
            var group = (sender as FrameworkElement).DataContext;
            this.Frame.Navigate(typeof(ItemsPage), ((DealGroup)group).Category);
        }

        /// <summary>
        /// Navigating to ItemDetails Page when clicking Deal item
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">agument</param>
        private void DealsGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var gid = ((Deal)e.ClickedItem).Gid;
            this.Frame.Navigate(typeof(ItemDetailsPage), gid);
        }
    }
}
